package com.futu.intelligence.controller;


import com.futu.intelligence.entity.TravelPlan;
import com.futu.intelligence.service.TravelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/travel")
public class TravelController {

    @Autowired
    private TravelService travelService;

    /**
     * 首页
     */
    @GetMapping("/")
    public String index() {
        return "index";
    }

    /**
     * 生成旅游计划页面
     */
    @GetMapping("/generate")
    public String generatePage() {
        return "generate";
    }

    /**
     * 生成旅游计划
     */
    @PostMapping("/generate")
    @ResponseBody
    public Map<String, Object> generatePlan(@RequestParam("city") String city,
                                            @RequestParam("days") int days,Long userId) {
        Map<String, Object> result = new HashMap<>();

        // 这里简化处理，假设用户ID为1


        TravelPlan plan = travelService.generatePlan(city, days, userId);

        if (plan != null) {
            result.put("success", true);
            result.put("planId", plan.getId());
        } else {
            result.put("success", false);
            result.put("message", "生成旅游计划失败");
        }

        return result;
    }

    /**
     * 查看旅游计划详情
     */
    @GetMapping("/plan/{id}")
    public String viewPlan(@PathVariable("id") Integer planId, Model model) {
        TravelPlan plan = travelService.getPlanDetail(planId);
        if (plan == null) {
            return "redirect:/travel/";
        }

        model.addAttribute("plan", plan);
        return "plan_detail";
    }

    /**
     * 收藏旅游计划
     */
    @PostMapping("/favorite")
    @ResponseBody
    public Map<String, Object> favoritePlan(@RequestParam("planId") Integer planId,Long userId) {
        Map<String, Object> result = new HashMap<>();

        // 这里简化处理，假设用户ID为1

        boolean success = travelService.favoritePlan(userId, planId);

        result.put("success", success);
        return result;
    }

    /**
     * 取消收藏
     */
    @PostMapping("/cancelFavorite")
    @ResponseBody
    public Map<String, Object> cancelFavorite(@RequestParam("planId") Integer planId,Long userId) {
        Map<String, Object> result = new HashMap<>();

        // 这里简化处理，假设用户ID为1


        boolean success = travelService.cancelFavorite(userId, planId);

        result.put("success", success);
        return result;
    }
}
